package com.plate.modules.system.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.plate.common.core.result.Result;
import com.plate.common.web.domain.LoginVal;
import com.plate.common.web.utils.OauthUtils;
import com.plate.modules.system.domain.bo.SysMenuQueryBO;
import com.plate.modules.system.domain.bo.SysMenuSaveBO;
import com.plate.modules.system.domain.po.SysMenu;
import com.plate.modules.system.domain.vo.RouterVo;
import com.plate.modules.system.service.ISysMenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName SysMenuController
 * @Description TODO
 * @Author jiaxd
 * @Date 2023年10月23日 22:28
 * @Version 1.0
 */

@RestController
@RequestMapping("/sys/sysMenu")
public class SysMenuController {

    @Autowired
    private ISysMenuService sysMenuService;

    @PostMapping("/list")
    public Result list(@RequestBody SysMenuQueryBO queryBO) {
        String userId = OauthUtils.getCurrentUser().getUserId();
        List<SysMenu> list = sysMenuService.getMuneList(queryBO, userId);
        return Result.SUCCESS(list);
    }

    @PostMapping("/tree")
    public Result tree() {
        List<SysMenu> list = sysMenuService.getMuneTree();
        return Result.SUCCESS(list);
    }

    @PostMapping("update")
    public Result update(@RequestBody SysMenuSaveBO saveBO) {
        if (StrUtil.isBlankIfStr(saveBO.getId())) {
            // 新增
            SysMenu sysMenu = new SysMenu();
            BeanUtils.copyProperties(saveBO, sysMenu);
            int save = sysMenuService.isSave(sysMenu);
        } else {
            // 修改
            SysMenu sysMenu = sysMenuService.getById(saveBO.getId());
            BeanUtils.copyProperties(saveBO, sysMenu);
            sysMenuService.isUpdate(sysMenu);
        }
        return Result.SUCCESS();
    }

    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable("id") Long id) {
        SysMenu sysMenu = sysMenuService.getById(id);
        if (ObjectUtil.isEmpty(sysMenu)) {
            return Result.FAIL("");
        }
        boolean b = sysMenuService.removeById(id);
        return Result.SUCCESS();
    }

    @PostMapping("getRouters")
    public Result getRouters() {
        LoginVal currentUser = OauthUtils.getCurrentUser();
        List<SysMenu> muneList = sysMenuService.getMuneList(new SysMenuQueryBO(), currentUser.getUserId());
        List<RouterVo> menus = sysMenuService.buildMenus(muneList);
        return Result.SUCCESS(menus);
    }

}
